---
title: "Cleaning DPI federalism"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load dpi dataset
  dip_fed_raw <- 
    import("../raw-data/x-dpi/DPI2020_stata13.dta")
```

"MUNI
Are municipal governments locally elected?
0 if neither local executive nor local legislature are locally elected. 1 if the executive is appointed, but the
legislature elected. 2 if they are both locally elected. No information, or no evidence of municipal
governments, is recorded as blank. If one source has information on a specific period, and the other has no
information on a different period, we do not extrapolate from one source to another; no information is always
recorded as blank. If there are multiple levels of sub-national government, we consider the lowest level as
the “municipal” level. This variable was extensively updated for this version, and as a result, the number of
non-missing observations has increased from 42% to 61%.

STATE
Are there state/province governments locally elected?
Recorded in the same manner as MUNI. If there are multiple levels of sub-national government, we consider
the highest level as the “state/province” level. Indirectly elected state/province governments, where directly
elected municipal bodies elect the state/province level, are not considered locally elected. Indirectly elected
state/province governments elected by directly elected state/province bodies are considered locally elected.
This variable was extensively updated for this version, and as a result, the number of non-missing
observations has increased from 66% to 77%."

# Clean DIP

```{r}
# clean
  dip_fed_clean <- 
    dip_fed_raw %>% 
    select(
      countryname, 
      year,
      state) %>%
    filter(!countryname %in% c("Turk Cyprus", "Yemen (AR)", "Yemen (PDR)", "eSwatini")) %>% # eSwatini duplicates Swaziland
    mutate(
      country = 
      recode(countryname,
        "Cent. Af. Rep." = "Central African Republic",
        "PRC" = "China",
        "Dom. Rep." = "Dominican Republic",
        "ROK" = "Republic of Korea",
        "PRK" = "North Korea",
        "GDR" = "East Germany",
        "S. Africa" = "South Africa",
        "P. N. Guinea" = "Papua New Guinea"),
      dpi_country_common = countryname(country),
      dpi_subnational_govt_elected = 
        case_when(
          state == 0 ~ 0, # No local elections 
          state == 1 ~ 1, # Legislature locally elected 
          state == 2 ~ 1, # Legislature and executive locally elected
          TRUE ~ NA_real_)) %>% 
    select(
      dpi_country_common,
      dpi_year = year,
      dpi_subnational_govt_elected)
```

# Save data

```{r}
  saveRDS(dip_fed_clean, "../cleaned-data/x-12-dpi-federalism.rds")
```
